/*
 */
package aptech.eproject.logictics.dao.kien;

import aptech.eproject.logictics.db.Branch;
import aptech.eproject.logictics.db.Vehicle;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Kien Nguyen <solidfoxrock at gmail.com>
 */
@Stateless
public class VehicleService implements VehicleServiceRemote {

    @PersistenceContext
    private EntityManager em;

    /**
     *
     * @param branchId
     * @return
     */
    @Override
    public Branch findBranch(int branchId) {
        return em.find(Branch.class, branchId);
    }

    /**
     * Find all vehicles belong to a branch
     * @param branchId Branch Id
     * @return list of {@code Vehicle} instances
     */
    @Override
    public List<Vehicle> findVehicles(int branchId) {
        Query q = em.createNamedQuery("findVehicles");
        q.setParameter(1, branchId);
        return q.getResultList();
    }

    /**
     * Find a vehicle by vehicle Id, which belongs to a branch
     * @param vehicleId Vehicle Id
     * @param branchId Branch Id
     * @return a {@code Vehicle} instance
     */
    @Override
    public Vehicle findVehicle(String vehicleId) {
        Query q = em.createNamedQuery("findVehicleById");        
        q.setParameter(1, vehicleId);
        return (Vehicle) q.getSingleResult();
    }

    /**
     * Find vehicles by vehicle type, which belong to a branch
     * @param type
     * @param branchId
     * @return list of {@code Vehicle} instances
     */
    @Override
    public List<Vehicle> findVehiclesByType(String type, int branchId) {
        Query q = em.createNamedQuery("findVehiclesByType");
        q.setParameter(1, branchId);
        q.setParameter(2, "%" + type + "%");
        return q.getResultList();
    }

    /**
     *
     * @param vehicle
     */
    @Override
    public void create(Vehicle vehicle) {
        Vehicle v = em.find(Vehicle.class, vehicle.getVehicleId());
        if (v != null && v.getIsDeleted()) {
            vehicle.setIsDeleted(false);
            em.merge(vehicle.getBranch());
            em.merge(vehicle);
        } else {
            em.persist(vehicle);
        }
    }

    /**
     *
     * @param vehicle
     */
    @Override
    public void edit(Vehicle vehicle) {
        em.merge(vehicle.getBranch());
        em.merge(vehicle);
    }

    /**
     * Remove a vehicle by setting isDeleted to True
     * @param vehicleId
     */
    @Override
    public void remove(String vehicleId) {
        Vehicle vehicle = em.find(Vehicle.class, vehicleId);
        if (vehicle == null) {
            throw new EntityNotFoundException("The vehicle with id " + vehicleId + " does not exist");
        }
        vehicle.setIsDeleted(true);
        em.merge(vehicle);
    }

    /**
     * Remove a list of vehicles by setting isDeleted to True
     * @param vehicleIds List of Vehicle Id
     */
    @Override
    public void remove(List<String> vehicleIds) {
        for (String vehicleId : vehicleIds) {
            Vehicle vehicle = em.find(Vehicle.class, vehicleId);
            if (vehicle == null) {
                throw new EntityNotFoundException("The vehicle with id " + vehicleId + " does not exist");
            }
            vehicle.setIsDeleted(true);
            em.merge(vehicle);
        }
    }

    /**
     * Recover a vehicle by setting isDeleted to False
     * @param vehicleId
     */
    @Override
    public void recover(String vehicleId) {
        Vehicle vehicle = em.find(Vehicle.class, vehicleId);
        if (vehicle == null) {
            throw new EntityNotFoundException("The vehicle with id " + vehicleId + " does not exist");
        }
        vehicle.setIsDeleted(false);
        em.merge(vehicle);
    }

    /**
     * For testing only
     * @param vehicleId
     */
    @Override
    public void removeForever(String vehicleId) {
        Vehicle vehicle = em.find(Vehicle.class, vehicleId);
        if (vehicle != null) {
            em.remove(vehicle);
        }
    }
}
